import React from "react";
import { Button,  Form, Divider,Col, Row} from 'antd/lib';
import notification from "@/services/notification";
import {get} from "lodash";
import Uploads from "@/services/uploads";
import Upload from "antd/lib/upload";
import 'antd/dist/antd.less'
import {StarOutlined, UploadOutlined} from '@ant-design/icons';
import { currentUser} from "@/services/auth";
import Attaches from "@/services/attaches";
import AttacheTab from "@/components/contract/new/AttacheTab";
export default class LookTable extends React.Component {
  state = {
    loading: true,
    id: 0,//
    annex:[],
    annexuncheck:[],//--附件
    annexchecking:[],//--附件
    annexchecked:[],//--附件
    annexlawyer:[],
    annexzj:[],//--附件
  };
  componentWillMount() {
     this.refreshData(this.props.id);
  }
  refreshData(id) {
    let params={
      page: 1,
      pagesize: 50,
      sourceid: id,
      sourcetype: "contracts",

    }
    Attaches.getlist(params).then(data => {
      if(data.data.length>0){
        let annexuncheck=[];
        let annexchecking=[];
        let annexchecked=[];
        let annexlawyer=[];
        let annex=[];
        data.data[0].uncheck.forEach(file => {
          if(file.url.indexOf("static/file/")<0){
            file.url='static/file/'+file.url;
          }
            if (annexuncheck.length < 1) {
              annexuncheck = [file];
            } else {
              annexuncheck.push.apply(annexuncheck, [file]);
            }
            if(annex.length<1){
              annex =[file];
            }else{
              annex.push.apply(annex, [file]);
            }
        });
        data.data[0].checking.forEach(file => {
          if(file.url.indexOf("static/file/")<0){
            file.url='static/file/'+file.url;
          }
            if (annexchecking.length < 1) {
              annexchecking = [file];
            } else {
              annexchecking.push.apply(annexchecking, [file]);
            }
            if(annex.length<1){
              annex =[file];
            }else{
              annex.push.apply(annex, [file]);
            }
        });
        data.data[0].checked.forEach(file => {
          if(file.url.indexOf("static/file/")<0){
            file.url='static/file/'+file.url;
          }
            if (annexchecked.length < 1) {
              annexchecked = [file];
            } else {
              annexchecked.push.apply(annexchecked, [file]);
            }
            if(annex.length<1){
              annex =[file];
            }else{
              annex.push.apply(annex, [file]);
            }
        });
        data.data[0].lawyer.forEach(file => {
          if(file.url.indexOf("static/file/")<0){
            file.url='static/file/'+file.url;
          }
            if (annexlawyer.length < 1) {
              annexlawyer = [file];
            } else {
              annexlawyer.push.apply(annexlawyer, [file]);
            }
        });
      this.setState({
        // annex: data.annex,
        annexuncheck:annexuncheck,//--附件
        annexchecking:annexchecking,//--附件
        annexchecked:annexchecked,//--附件
        annexlawyer: annexlawyer,
        loading: false,
        annex: annex,
      });
    }
    }).catch(error => {
          this.setState({
            annexuncheck:[],//--附件
            annexchecking:[],//--附件
            annexchecked:[],//--附件
            annexlawyer:[],
            annex:[],
            loading: false,
          });
    });
    Attaches.get({sourceid:id,type:"contractszj" }).then(data => {
      this.setState({
        id: data.id,
        annexzj: data.annex,
        loading: false,
      });
    }).catch(error => {
          this.setState({
            id: 0,
            annexzj:[],//--附件
            loading: false,
          });
    });
  }
  // ===================================zhuijiafujian===================
handleRemovezj(file){
    let filename=file.url;
    Uploads.removefile({filename: filename}).then(data => {
         if(data.code==="sucess"){
            notification.success(data.msg);
         }
         else{
          notification.error(data.msg);
          }
        }).catch(error => {
          notification.error(get(error, "response.data.message", "删除失败."));
        });
  };
   handledownzj(file){
     let  filename=file.url;
     let AjaxUrl = 'api/upload/downloadfile';
     fetch(AjaxUrl ,{
      method: 'POST',
      body: window.JSON.stringify({filename: filename}),
      credentials: 'include',
      headers: new Headers({
        'Content-Type': 'application/json'
      })}).then((response) => {
      response.blob().then(blob => {
        const aLink = document.createElement('a');
        document.body.appendChild(aLink);
        aLink.style.display='none';
        const objectUrl = window.URL.createObjectURL(blob);
        aLink.href = objectUrl;
        aLink.download = file.name;
        aLink.click();
        document.body.removeChild(aLink);
      });
    }).catch((error) => {
      console.log(error);
    });
  };

  render() {
    if (this.state.loading) return null;
    return(
      <div>
      <AttacheTab annex={this.state.annex} annexlawyer={this.state.annexlawyer} id={this.props.id} status ={this.props.status} 
          isedit={true} colcontrol={false} billtype="contracts"
          onRef={(ref) => { this.childattache = ref; }}  childchange3 = {this.childchange3}
          />
      </div>
    );
  };
}

